#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'
require_relative '../spec/commons/check_pages'
require_relative '../spec/commons/employee_edit'

describe 'Employees page' do
  
  before {

  }

  context 'when logged in' do
  	it 'then he can work with employees list page' do
  	  goto 'http://pulse.7bits.it/en/login-page'
  	  sign_in("adm.company2@mail.ru","123qwe+")
  	  goto 'http://pulse.7bits.it/en/us/admin/employees-list'
  	  select_list(id: "js-admin-selector-departments", class: "form-control min-width-selector").select 'cats'      
      tableT = table(class: "table table-hover table-employees table-striped js-admin-table-for-department")
      employeeId = tableT.tbody.trs[1].attribute_value('data-employee-id')
      nameML = (tableT.tbody.trs[1].attribute_value('data-employee-name')).split(' ')
     
      managerId = findManagerId(1, tableT)
      departmentId = findDepartmentId()
      role = findRole(1,tableT)

      goto "http://pulse.7bits.it/en/us/admin/edit-employee?userId=#{employeeId.to_s}"
      check_employee_edit(employeeId, nameML[0], nameML[1], nameML[2],"manager.kir@mail.ru", managerId, departmentId, role)
      end
  end

  context "when submit employee first name" do
    it "then it can't be blank" do
      go_employee(1)
      text_field(id: "employee-input-firstName", class: "form-control").clear      
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      (browser.text.include? "First name can't be blank").should be_truthy
      span(class: "help-block", text: "First name can't be blank").should be_present
    end
  end
  
  context "when submit employee first name" do
    it "then it should contain no more then 255 symbols" do
      go_employee(1)
      upstr='Q'
      256.times{|i| upstr+='a'}
      text_field(id: "employee-input-firstName", class: "form-control").clear      
      text_field(id: "employee-input-firstName", class: "form-control").set upstr
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "First name should contain no more than 255 symbols").should be_present
    end
  end

 #middle name  
  context "when submit employee middle name" do
    it "then it should contain no more then 255 symbols" do
      go_employee(1)
      upstr='Q'
      256.times{|i| upstr+='a'}
      text_field(id: "employee-input-middleName", class: "form-control").clear
      text_field(id: "employee-input-middleName", class: "form-control").set upstr
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Middle name should contain no more than 255 symbols").should be_present
    end
  end

#last name
  context "when submit  employee last name" do
    it "then it can't be blank" do
      go_employee(1)
      text_field(id: "employee-input-lastName", class: "form-control").clear
      text_field(id: "employee-input-lastName", class: "form-control").set ""
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Last name can't be blank").should be_present
    end
  end
  
  context "when submit employee last name" do
    it "then it should contain no more then 255 symbols" do
      go_employee(1)
      upstr='Q'
      256.times{|i| upstr+='a'}
      text_field(id: "employee-input-lastName", class: "form-control").clear
      text_field(id: "employee-input-lastName", class: "form-control").set upstr
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Last name should contain no more than 255 symbols").should be_present
    end
  end

  context "when submit employee email" do
    it "then it can't be blank" do
      go_employee(1)
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").clear      
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Invalid email").should be_present
    end
  end

  context "when submit employee email" do
    it "then it should be valid " do
      ####
      #   проверить в старом браузере
      ####
      go_employee(1)
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").clear      
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").set "123456ru"
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      #span(class: "help-block", text: "Invalid email").should be_present
    end
  end 

  context "when submit employee email" do
    it "then it should contain no more then 255 symbols" do
      go_employee(1)
      upstr='Q'
      256.times{|i| upstr+='a'}
      upstr = upstr + "@are.ru"
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").clear      
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").set upstr
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Email should contain no more than 255 symbols").should be_present
    end
  end

  context "when submit email" do
  it "then it must be new" do
      url = browser.url
      goto 'http://pulse.7bits.it/en/us/admin/employees-list'
      select_list(id: "js-admin-selector-departments", class: "form-control min-width-selector").select 'cats'      
      tableT = table(class: "table table-hover table-employees table-striped js-admin-table-for-department")
      i = findNameColumn("Email", tableT)
      another_email = tableT.tbody[0][i].text      
      goto url
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").clear      
      text_field(id: "employee-input-email", class: "form-control", type: "email", name: "email", placeholder: "Enter email").set another_email
      button(class: "btn btn-success", type: "submit", text: "Submit").click
      span(class: "help-block", text: "Employee with such email is already exist").should be_present           
    end
  end

  
  context 'when logged out' do
    it "then he can't work with employees list page"  do
    user_logout()
    end
  end
    
end